﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FrmLogin
{
    public partial class Form2 : Form
    {
        clsFunciones _metodos = new clsFunciones();
        DataTable _dtPermisos = new DataTable();
        MdiClient mdi = null;
        
        public Form2()
        {
            InitializeComponent();
            foreach (Control c in this.Controls)
            {
                if (c is MdiClient)
                {
                    mdi = (MdiClient)c;
                    break;
                }
            }
        }

        private void LoadForm(object form)
        {
            foreach (Form f in mdi.MdiChildren) f.Close();
            if (form == null)
                return;
            ((Form)form).MdiParent = this;
            ((Form)form).Show();            
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            Application.EnableVisualStyles();
            DatosUsuario();
        }

        private void DatosUsuario()
        {
            try
            {
                if (GlobalVars.EsAdmin) HabilitarMenu(true);
                else ValidarPermisosUsuario();
                StatusLabelUsuario.Text = String.Format("{0} {1}", GlobalVars.Nombre, GlobalVars.Apellido);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void HabilitarMenu(bool Habilitar)
        {
            foreach (RibbonTab rTabs in ribbon1.Tabs)
            {
                rTabs.Visible = Habilitar;
                foreach (RibbonPanel rPanels in rTabs.Panels)
                {
                    rPanels.Visible = Habilitar;
                    foreach (RibbonButton rButtons in rPanels.Items)
                    {                        
                        rButtons.Visible = Habilitar;
                    }
                }                
            }
        }

        private void ValidarPermisosUsuario()
        {
            try
            {
                _dtPermisos = _metodos.GetDataByIdUsuario(GlobalVars.IdUsuario);
                bool _flag;
                int _contPanel = 0;
                int _contTab = 0;
                foreach (RibbonTab rTabs in ribbon1.Tabs)
                {
                    _contTab = 0;
                    foreach (RibbonPanel rPanels in rTabs.Panels)
                    {
                        _contPanel = 0;
                        foreach (RibbonButton rButtons in rPanels.Items)
                        {
                            _flag = false;
                            foreach (DataRow drPermiso in _dtPermisos.Rows)
                            {
                                if (rButtons.Tag.ToString() == drPermiso[1].ToString())
                                {
                                    rButtons.Visible = true;
                                    _flag = true;
                                    _contTab += 1;
                                    _contPanel += 1;
                                    break;
                                }
                            }
                            if (!_flag) rButtons.Visible = false;                         
                        }
                        if (_contPanel == 0) rPanels.Visible = false; 
                    }
                    if (_contTab == 0) rTabs.Visible = false;      
                }
            }
            catch (Exception ex)
            {
               MessageBox.Show(ex.Message);
            }
        }

        private void rb_SegCambiarUser_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Esta seguro de cambiar de usuario?", "Mensaje de Confirmación", MessageBoxButtons.YesNo, 
                MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            {
                Form[] Formularios = null; 
                Formularios = this.MdiChildren;
                foreach (Form Formulario in Formularios) Formulario.Close(); 
                StatusLabelUsuario.Text = "No hay sesión activa!!!";
                //Instanciar el formulario de inicio de sesion
                var _frmLogin = new Form1();
                _frmLogin.ShowDialog();
                if (_frmLogin.DialogResult == DialogResult.OK) DatosUsuario();
            }
        }       

        private void rb_SegUsuarios_Click(object sender, EventArgs e)
        {
            LoadForm(new Form3());     
        }

        private void rb_SegPermisosUsuarios_Click(object sender, EventArgs e)
        {
            LoadForm(new Form5());   
        }
    }
}
